#ifndef __DAHUA_MULTICAMERAS_DEMO_MVIMAGE_CONVERT_H__
#define __DAHUA_MULTICAMERAS_DEMO_MVIMAGE_CONVERT_H__

#include "MvImageBuf.h"
#include "GenICam/System.h"
#include "Infra/Thread.h"
#include "Infra/Mutex.h"
#include "Infra/Semaphore.h"
#include "Memory/SharedPtr.h"

#include <list>

namespace Dahua{

#define TIMESTAMPFREQUENCY 125000000	//大华相机的时间戳频率固定为125,000,000Hz

class CMvImageConvertHelper
{
public:
	CMvImageConvertHelper(const GenICam::ICameraPtr& sptrCamera);
	~CMvImageConvertHelper();

public:
	static unsigned int __stdcall threadProc(void* pParam);

public:
	bool isValid();

	bool startConvert(int iConvertRate);

	bool stopConvert();

	CMvImageBufPtr getConvertedImage();

	void setDisplayFPS(int nFPS);

private:
    void destroyThread();

	void grabbingThreadProc(Infra::CThreadLite*);

	bool isTimeToDisplay();

	bool convertFrame(const GenICam::CFrame& frameBuf);

	int findMatchCode(int iCode);

	uint32_t calcRateCtrlNum(uint32_t iFrameRate);

	void addConvertedImage(const CMvImageBufPtr& sptrConvertedImage);

private:
	typedef std::list<CMvImageBufPtr> ImageListType;

private:
	GenICam::ICameraPtr				m_sptrCamera;
	GenICam::IStreamSourcePtr		m_sptrStreamSrc;
	ImageListType					m_listImages;
	bool							m_bRunning;
//	Infra::CThreadLite				m_grabbingThread;
	Infra::CMutex					m_mutexQue;
	Infra::CSemaphore				m_semQue;
	uint32_t						m_iFrameRate;
	uint32_t						m_iCalcTimeBegin;
	uint32_t						m_iCalcTimeEnd;
	uint32_t						m_iFrameCount;
	uint32_t						m_iControlNum;

	Infra::CMutex					m_mxTime;
	int							m_dDisplayInterval;         // 显示间隔
	intmax_t						m_nTimestampFreq;           // 时间戳频率
	intmax_t						m_nFirstFrameTime;          // 第一帧的时间戳
	intmax_t						m_nLastFrameTime;           // 上一帧的时间戳

	int								m_width;
	int								m_height;
	int								m_paddingX;
	int								m_paddingY;
	uint32_t						m_pixelFormat;
	uint32_t						m_dataSize;

    HANDLE                          m_hThread;
};

typedef Memory::TSharedPtr<CMvImageConvertHelper> CMvImageConvertHelperPtr;

} // namespace Dahua

#endif // __DAHUA_MULTICAMERAS_DEMO_MVIMAGE_CONVERT_H__
